<template>
  <div class="businessMask">
    <div class="businessDialog" style="width:450px; height: 600px;">
      <div class="dialog-header">
        <span>角色选择</span>
      </div>
      <div class="dialog-body">
        <el-tree
            lazy
            ref="tree"
            node-key="code"
            :props="treeProps"
            :load="loadNode"
            :show-checkbox="true"
            :expand-on-click-node="false"
            icon-class="icon iconfont iconkuangjia-xiala1"
            @check="handleCheckChange"
        >
        </el-tree>
      </div>
      <div class="dialog-footer right">
        <span class="businessButtonConfirm" @click="confirm">确定</span>
        <span class="businessButtonCancel" @click="cancel">取消</span>
      </div>
    </div>
  </div>
</template>

<script>
import {projectDeptTree} from '@/business/api/projectDept'

export default {
  data() {
    return {
      treeProps: {
        label: 'name',
        isLeaf: 'leaf',
        disabled: function (data) {
          return data.type !== 3
        },
      },
      treeParam: {
        type: 0, // 树父节点类型：0-加载项目部，1-（项目部）加载角色，2-(角色)加载人员
        parentId: 0, // 父级id，加载角色、用户必输
        code: null // 角色code，加载人员必输
      },
      checkedList: []
    }
  },
  props: {
    params: {
      autType: null, // 权限类型：1-本单位过滤，2-权限过滤
      funcCode: null, // 菜单编码，加载项目部时必输
      checkbox: null // 勾选框：默认多选，1-单选
    }
  },
  created() {
    this.treeParam = {...this.treeParam, ...this.params}
  },
  methods: {
    // 树 懒加载
    loadNode(node, resolve) {
      if (node.level > 0) {
        this.treeParam.parentId = node.data.id
        this.treeParam.type = node.data.type
        this.treeParam.code = node.data.code
      }
      projectDeptTree(this.treeParam).then((res) => {
        if (res.code == 200) {
          if (res.success) {
            this.treeData = res.info
            return resolve(res.info)
          } else this.message(res.msg, 3)
        } else this.message(res.msg, 3)
      })
    },
    handleCheckChange(data, node) {
      this.checkedList = []
      if (this.params.checkbox === 1 && node.checkedNodes.length > 1) {
        this.$refs.tree.setCheckedKeys([])
        this.$refs.tree.setChecked(data, true)
        this.checkedList = data
      } else {
        node.checkedNodes.forEach((item) => {
          if (item.type === 3) {
            this.checkedList.push(item)
          }
        })
      }
    },
    confirm() {
      this.$emit('confirm', this.checkedList)
    },
    cancel() {
      this.$emit('cancel')
    }
  }
}
</script>

